package com.ereport.controller.adminController;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.ADPDAO;
import com.ereport.dao.AccountsDAO;
import com.ereport.dao.AdminExeBudgetDAO;
import com.ereport.dao.ExecutiveDAO;
import com.ereport.dao.LoginDetDAO;
import com.ereport.entity.ADP;
import com.ereport.entity.Accounts;
import com.ereport.entity.AdminExeBudget;
import com.ereport.entity.Executives;
import com.ereport.entity.LoginDet;
import com.ereport.util.SendMail;

@Controller
@RequestMapping("/exeRegistration")
public class ExecutiveRegstrController {
	@RequestMapping("/open.do")
	public String open(HttpServletRequest req,HttpSession session,ModelMap model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			return "/jsp/login.jsp";
		}
		ExecutiveDAO exdao=new ExecutiveDAO();
		List<Executives> exlist=null;
		try {
			exlist=exdao.getExecutives();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		StringBuilder sb=new StringBuilder();
		
		if(exlist!=null){
			for(int i=0;i<exlist.size();i++){
				String exname=exlist.get(i).getExeName();
				sb.append(exname+"/");
			}
		}
		
		model.addAttribute("exelist", sb.toString());
		return "/jsp/admin/executiveRegistration.jsp";
	}
	@RequestMapping(value="/save.do", method=RequestMethod.POST)
	public ModelAndView insert(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		String exname=req.getParameter("exeName");
		String permaddress=req.getParameter("permaddress");
		String presaddress=req.getParameter("presaddress");
		String exeEmail=req.getParameter("exeEmail");
		String exePass=req.getParameter("exePass");
		String exePhone=req.getParameter("exePhone");
		String exeGender=req.getParameter("exeGender");
		System.out.println("gender "+exeGender);
		
		java.util.Date crdate= new java.util.Date();
		java.sql.Date dt=null;
		dt=new java.sql.Date(crdate.getTime());
		ExecutiveDAO edao=new ExecutiveDAO();
		
		Executives exec=new Executives();
		String exid=req.getParameter("exid");
		System.out.println("exeid "+exid);
		
		
		if(exid!=null){
			int eid=Integer.parseInt(exid);
						
			try {
				exec=new Executives(eid,exname,permaddress,presaddress,exeEmail,exePass,exePhone,exeGender,dt);
			System.out.println("exe obj "+exec);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			LoginDet ldet=null;
			LoginDetDAO ldao=new LoginDetDAO();
			try {
				ldet=ldao.getLastLogin(eid);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			//java.util.Date date= new java.util.Date();
			Set<LoginDet> lgset=new HashSet<LoginDet>();
			System.out.println("log id "+ldet.getId()+" exe id "+ldet.getExeId()+" lastlogin "+ldet.getLastLogin());
			ldet=new LoginDet(ldet.getId(),ldet.getExeId(),ldet.getLastLogin());
			lgset.add(ldet);
			exec.setLogdt(lgset);
			try {
				edao.insert(exec);
			} catch (Exception e) { 
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
					
	}else{
		
		
		try {
			exec=new Executives(exname,permaddress,presaddress,exeEmail,exePass,exePhone,exeGender,dt);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		LoginDet ldet=null;
		java.util.Date date= new java.util.Date();
		Set<LoginDet> lgset=new HashSet<LoginDet>();
		ldet=new LoginDet(new Timestamp(date.getTime()));
		lgset.add(ldet);
		exec.setLogdt(lgset);
		try {
			edao.insert(exec);
		} catch (Exception e) { 
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		model.setViewName("redirect:open.do");
		model.addObject("success", "Successfully Inserted");
		return model;
	}
	@RequestMapping(value="/emailValidate.do", method=RequestMethod.GET)
	public @ResponseBody
	String emailValid(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			
			return "/jsp/login.jsp";
		}
		String email=req.getParameter("email");
		ExecutiveDAO exdao=new ExecutiveDAO();
		Executives exe=null;
		try {
			exe=exdao.emailValid(email);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(exe!=null){
			
			return "false";
		}else{
			return "true";
		}
		
	}
	@RequestMapping(value="/getExeDetails.do", method=RequestMethod.POST)
	public ModelAndView getExeDetails(HttpServletRequest req,HttpSession session, ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String exeName=req.getParameter("execName");
		ExecutiveDAO exdao=new ExecutiveDAO();
		Executives exe=null;
		try {
			exe=exdao.getExecutive(exeName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(exe!=null){
				
		model.setViewName("/jsp/admin/editExecutiveRegistration.jsp");
		model.addObject("execdet", exe);
		return model;
		}else{
			model.setViewName("redirect:open.do");
			model.addObject("execdet", null);
			return model;
		}
		
	}
	@RequestMapping(value="/sendPwd.do", method=RequestMethod.POST)
	public ModelAndView sendPwd(HttpServletRequest req,HttpSession session, ModelAndView model){
		String email=req.getParameter("useremail");
		ExecutiveDAO edao=new ExecutiveDAO();
		Executives ex=null;
		try {
			ex=edao.getPassword(email);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(ex!=null){
		String pwdtext="Your Password is "+ex.getPwd();
		String frmEmail="advaithsofttest@gmail.com";
		String frmPwd="advaithsofttest";
		String subj="Received Password";
		SendMail smail=new SendMail();
		String retrnmsg=smail.sendMail(frmEmail,frmPwd,pwdtext, subj,email);
		
		model.addObject("emailMsg",retrnmsg );
		}else{
			model.addObject("emailMsg","<br/>Wrong Email Address <br/> or <br/> Please Check Your Internet Connection" );
		}
		model.setViewName("/jsp/login.jsp");
		return model;
		
	}
	@RequestMapping(value="/getExec.do",method = RequestMethod.GET)
	public @ResponseBody
	String getClientDet(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		ExecutiveDAO exdao=new ExecutiveDAO();
		StringBuilder sb=new StringBuilder();
		List<Executives> exlist=exdao.getExecutives();
		Iterator itr=exlist.iterator();
		while(itr.hasNext()==true){
			Object ob=itr.next();
			Executives cli=(Executives)ob;
			sb.append(cli.getExeName());
			sb.append(",");
		}
		return sb.toString();
	}
}
